package cn.cdutetc.foxwatch.controller;

import cn.cdutetc.foxwatch.mapper.DemoMapper;
import cn.cdutetc.foxwatch.pojo.DemoPojo;
import cn.cdutetc.foxwatch.pojo.PagePojo;
import cn.cdutetc.foxwatch.pojo.Result;
import cn.cdutetc.foxwatch.query.DemoQuery;
import cn.cdutetc.foxwatch.serveice.DemoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@RequestMapping("/demo")
@Tag(name = "演示接口", description = "这是演示接口")
public class DemoController {
    @Autowired
    private DemoService demoService;
    @Autowired
    private DemoMapper demoMapper;

    //   分页查询
    @GetMapping
    @Operation(summary = "分页查询", description = "分页查询")
<<<<<<< HEAD
    public Result<Object> get(@RequestParam(value = "name", required = false) String name,
                              @RequestParam("pageNum") Integer pageNum,
                              @RequestParam("pageSize") Integer pageSize
    ) {
        System.out.println(pageNum);
        System.out.println(pageSize);
        System.out.println(name);
        DemoQuery demoQuery = new DemoQuery(pageNum, pageSize, "create_time", false, name);
=======
    public Result<Object> get(DemoQuery demoQuery) {
>>>>>>> f0dd41dd48d79067fa2de22e427243c250479df2
        PagePojo<DemoPojo> result = demoService.queryDemosPage(demoQuery);
        return Result.success(result);
    }

    //批量新增数据
    @PostMapping
    @Operation(summary = "批量新增数据", description = "批量新增数据")
    public Result<String> post(@RequestBody ArrayList<DemoPojo> demoList) {
        System.out.println(demoList);
        boolean b = demoService.saveBatch(demoList);
        return b ? Result.success("success") : Result.error("error");
    }

    //批量删除数据根据id
    @DeleteMapping
    @Operation(summary = "批量删除数据", description = "批量删除数据")
    public Result<String> delete(@RequestBody Integer[] ids) {
        System.out.println(ids);
        if (ids.length == 0) {
            return Result.error("参数不能为空");
        }
        QueryWrapper<DemoPojo> wrapper = new QueryWrapper<>();
        wrapper.in("id", ids);
        int delete = demoMapper.delete(wrapper);
        if (delete == ids.length)
            return Result.success("success");
        else return Result.error("error");
    }

    //批量修改数据
    @PutMapping
    @Operation(summary = "批量修改数据", description = "批量修改数据")
    public Result<String> put(@RequestBody ArrayList<DemoPojo> demoList) {
        System.out.println(demoList);
        UpdateWrapper<DemoPojo> wrapper = new UpdateWrapper<>();
        for (DemoPojo demoPojo : demoList) {
            wrapper.eq("id", demoPojo.getId());
            int update = demoMapper.update(demoPojo, wrapper);
            if (update != 1) {
                return Result.error("error");
            }
        }
        return Result.success("success");
    }
}
